<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DeepSeek AI 聊天助手</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            color: #333;
            height: 100vh;
            overflow: hidden;
        }

        .container {
            display: flex;
            height: 100vh;
            max-width: 1600px;
            margin: 0 auto;
            box-shadow: 0 0 30px rgba(0, 0, 0, 0.3);
        }

        /* 聊天区域样式 */
        .chat-area {
            flex: 3;
            display: flex;
            flex-direction: column;
            background-color: rgba(255, 255, 255, 0.95);
            border-radius: 10px 0 0 10px;
        }

        .chat-header {
            padding: 20px;
            background: linear-gradient(90deg, #4b6cb7, #182848);
            color: white;
            border-radius: 10px 0 0 0;
            display: flex;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .chat-header h1 {
            font-size: 1.5rem;
            margin-left: 15px;
        }

        .logo {
            width: 40px;
            height: 40px;
            background-color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            color: #4b6cb7;
        }

        .messages-container {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            background-color: #f8f9fa;
        }

        .message {
            margin-bottom: 20px;
            display: flex;
            align-items: flex-start;
        }

        .message.user {
            justify-content: flex-end;
        }

        .message-content {
            max-width: 70%;
            padding: 12px 18px;
            border-radius: 18px;
            position: relative;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .message.user .message-content {
            background: linear-gradient(135deg, #4b6cb7, #182848);
            color: white;
            border-bottom-right-radius: 5px;
        }

        .message.ai .message-content {
            background-color: white;
            border: 1px solid #e0e0e0;
            border-bottom-left-radius: 5px;
        }

        .message-avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
            font-weight: bold;
            flex-shrink: 0;
        }

        .message.user .message-avatar {
            order: 2;
            margin-right: 0;
            margin-left: 10px;
            background-color: #4b6cb7;
            color: white;
        }

        .message.ai .message-avatar {
            background-color: #f0f0f0;
            color: #4b6cb7;
        }

        .input-area {
            padding: 20px;
            background-color: white;
            border-top: 1px solid #e0e0e0;
            display: flex;
            border-radius: 0 0 0 10px;
        }

        .message-input {
            flex: 1;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 24px;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s;
        }

        .message-input:focus {
            border-color: #4b6cb7;
        }

        .send-button {
            background: linear-gradient(135deg, #4b6cb7, #182848);
            color: white;
            border: none;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            margin-left: 10px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .send-button:hover {
            transform: scale(1.05);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }

        .send-button:active {
            transform: scale(0.98);
        }

        .send-button:disabled {
            background: #cccccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        /* 信息面板样式 */
        .info-panel {
            flex: 1;
            background: linear-gradient(135deg, #182848, #1a2a6c);
            color: white;
            padding: 30px;
            border-radius: 0 10px 10px 0;
            display: flex;
            flex-direction: column;
        }

        .panel-title {
            font-size: 1.5rem;
            margin-bottom: 30px;
            text-align: center;
            position: relative;
        }

        .panel-title:after {
            content: '';
            display: block;
            width: 60px;
            height: 3px;
            background: linear-gradient(90deg, #fdbb2d, #b21f1f);
            margin: 10px auto 0;
            border-radius: 3px;
        }

        .feature-list {
            list-style: none;
            margin-bottom: 30px;
        }

        .feature-list li {
            padding: 12px 0;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            display: flex;
            align-items: center;
        }

        .feature-list li:before {
            content: '✓';
            margin-right: 10px;
            color: #4b6cb7;
            font-weight: bold;
        }

        .api-status {
            margin-top: auto;
            padding: 15px;
            background-color: rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            text-align: center;
        }

        .status-indicator {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 8px;
        }

        .status-connected {
            background-color: #4CAF50;
        }

        .status-disconnected {
            background-color: #f44336;
        }

        /* 加载动画 */
        .typing-indicator {
            display: flex;
            align-items: center;
            padding: 12px 18px;
            background-color: white;
            border-radius: 18px;
            border-bottom-left-radius: 5px;
            max-width: 70%;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            border: 1px solid #e0e0e0;
        }

        .typing-dot {
            width: 8px;
            height: 8px;
            background-color: #999;
            border-radius: 50%;
            margin: 0 2px;
            animation: typing 1.4s infinite ease-in-out;
        }

        .typing-dot:nth-child(1) {
            animation-delay: 0s;
        }

        .typing-dot:nth-child(2) {
            animation-delay: 0.2s;
        }

        .typing-dot:nth-child(3) {
            animation-delay: 0.4s;
        }

        @keyframes typing {
            0%, 60%, 100% {
                transform: translateY(0);
            }
            30% {
                transform: translateY(-10px);
            }
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                flex-direction: column;
            }

            .chat-area {
                border-radius: 0;
                flex: 1;
            }

            .info-panel {
                border-radius: 0;
                display: none; /* 在移动设备上隐藏信息面板 */
            }

            .message-content {
                max-width: 85%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 聊天区域 -->
        <div class="chat-area">
            <div class="chat-header">
                <div class="logo">AI</div>
                <h1>DeepSeek AI 聊天助手</h1>
            </div>

            <div class="messages-container" id="messages-container">
                <!-- 初始欢迎消息 -->
                <div class="message ai">
                    <div class="message-avatar">AI</div>
                    <div class="message-content">
                        您好！我是DeepSeek AI助手，很高兴为您服务。请问有什么可以帮助您的吗？
                    </div>
                </div>
            </div>

            <div class="input-area">
                <input type="text" class="message-input" id="message-input" placeholder="输入您的问题..." autocomplete="off">
                <button class="send-button" id="send-button">
                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M2 21L23 12L2 3V10L17 12L2 14V21Z" fill="white"/>
                    </svg>
                </button>
            </div>
        </div>

        <!-- 信息面板 -->
        <div class="info-panel">
            <h2 class="panel-title">AI助手功能</h2>

            <ul class="feature-list">
                <li>智能对话与问题解答</li>
                <li>代码编写与调试帮助</li>
                <li>学习辅导与知识解答</li>
                <li>创意写作与头脑风暴</li>
                <li>多语言翻译与解释</li>
                <li>数据分析与处理建议</li>
            </ul>
            <div class="api-status">
                <span class="status-indicator status-connected"></span>
                <span id="status-text">API已连接</span>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const messagesContainer = document.getElementById('messages-container');
            const messageInput = document.getElementById('message-input');
            const sendButton = document.getElementById('send-button');
            const statusIndicator = document.getElementById('status-indicator');
            const statusText = document.getElementById('status-text');

            // 测试API连接状态
            checkApiStatus();

            // 发送消息函数
            function sendMessage() {
                const message = messageInput.value.trim();
                if (!message) return;

                // 添加用户消息到聊天界面
                addMessage(message, 'user');
                messageInput.value = '';

                // 显示AI正在输入
                showTypingIndicator();

                // 发送请求到后端
                fetch('/chat/', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': getCookie('csrftoken')
                    },
                    body: JSON.stringify({ message: message })
                })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('网络响应异常');
                    }
                    return response.json();
                })
                .then(data => {
                    // 移除正在输入指示器
                    removeTypingIndicator();

                    if (data.error) {
                        addMessage('抱歉，发生了错误：' + data.error, 'ai');
                    } else {
                        addMessage(data.message, 'ai');
                    }
                })
                .catch(error => {
                    // 移除正在输入指示器
                    removeTypingIndicator();

                    console.error('错误:', error);
                    addMessage('抱歉，网络连接出现问题，请稍后重试。', 'ai');
                });
            }

            // 添加消息到聊天界面
            function addMessage(content, sender) {
                const messageDiv = document.createElement('div');
                messageDiv.className = `message ${sender}`;

                const avatarDiv = document.createElement('div');
                avatarDiv.className = 'message-avatar';
                avatarDiv.textContent = sender === 'user' ? '您' : 'AI';

                const contentDiv = document.createElement('div');
                contentDiv.className = 'message-content';
                contentDiv.textContent = content;

                if (sender === 'user') {
                    messageDiv.appendChild(contentDiv);
                    messageDiv.appendChild(avatarDiv);
                } else {
                    messageDiv.appendChild(avatarDiv);
                    messageDiv.appendChild(contentDiv);
                }

                messagesContainer.appendChild(messageDiv);
                scrollToBottom();
            }

            // 显示AI正在输入
            function showTypingIndicator() {
                const typingDiv = document.createElement('div');
                typingDiv.className = 'typing-indicator';
                typingDiv.id = 'typing-indicator';

                for (let i = 0; i < 3; i++) {
                    const dot = document.createElement('div');
                    dot.className = 'typing-dot';
                    typingDiv.appendChild(dot);
                }

                messagesContainer.appendChild(typingDiv);
                scrollToBottom();
            }

            // 移除正在输入指示器
            function removeTypingIndicator() {
                const typingIndicator = document.getElementById('typing-indicator');
                if (typingIndicator) {
                    typingIndicator.remove();
                }
            }

            // 滚动到底部
            function scrollToBottom() {
                messagesContainer.scrollTop = messagesContainer.scrollHeight;
            }

            // 检查API连接状态
            function checkApiStatus() {
                fetch('/test_api_connection/')
                .then(response => response.json())
                .then(data => {
                    if (data.status === 'success') {
                        statusIndicator.className = 'status-indicator status-connected';
                        statusText.textContent = 'API连接正常';
                    } else {
                        statusIndicator.className = 'status-indicator status-disconnected';
                        statusText.textContent = 'API连接异常';
                    }
                })
                .catch(error => {
                    console.error('API状态检查失败:', error);
                    statusIndicator.className = 'status-indicator status-disconnected';
                    statusText.textContent = 'API连接异常';
                });
            }

            // 获取CSRF令牌
            function getCookie(name) {
                let cookieValue = null;
                if (document.cookie && document.cookie !== '') {
                    const cookies = document.cookie.split(';');
                    for (let i = 0; i < cookies.length; i++) {
                        const cookie = cookies[i].trim();
                        if (cookie.substring(0, name.length + 1) === (name + '=')) {
                            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                            break;
                        }
                    }
                }
                return cookieValue;
            }

            // 事件监听
            sendButton.addEventListener('click', sendMessage);

            messageInput.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    sendMessage();
                }
            });

            // 输入框自动聚焦
            messageInput.focus();
        });
    </script>
</body>
</html>